package com.wanmait.baohan.service.Impl;

import com.github.pagehelper.PageInfo;
import com.wanmait.baohan.mapper.WorkTemplateExecutableMapper;
import com.wanmait.baohan.pojo.DailyPlan;
import com.wanmait.baohan.pojo.WorkTemplateExecutable;
import com.wanmait.baohan.service.DailyPlanService;
import com.wanmait.baohan.service.WorkTemplateExecutableService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * TODO
 *
 * @author 打豆豆
 * @version 1.0
 * @date 2021/2/22 21:33
 * @description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WorkTemplateExecutableServiceImpl implements WorkTemplateExecutableService {
    @Resource
    private WorkTemplateExecutableMapper workTemplateExecutableMapper;
    @Resource
    private DailyPlanService dailyPlanService;

    @Override
    public void insert(WorkTemplateExecutable object) {

    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public void delete(String ids) {

    }

    @Override
    public void update(WorkTemplateExecutable object) {

    }

    @Override
    public WorkTemplateExecutable findById(Integer id) {
        return null;
    }

    @Override
    public List<WorkTemplateExecutable> findAll() {
        return null;
    }

    @Override
    public List<WorkTemplateExecutable> findAll(WorkTemplateExecutable object) {
        return null;
    }

    @Override
    public PageInfo<WorkTemplateExecutable> findPage(int pageNum) {
        return null;
    }

    @Override
    public PageInfo<WorkTemplateExecutable> findPage() {
        return null;
    }

    @Override
    public PageInfo<WorkTemplateExecutable> findPage(int pageNum, WorkTemplateExecutable object) {
        return null;
    }

    @Override
    public PageInfo<WorkTemplateExecutable> findPage(WorkTemplateExecutable object) {
        return null;
    }

    @Override
    public void insertList (List<WorkTemplateExecutable> workTemplateExecutables){
        workTemplateExecutables.forEach(workTemplateExecutable ->{

              workTemplateExecutableMapper.insert(workTemplateExecutable);

              if ("只一次".equals(workTemplateExecutable.getWorkTemplateContent().getTime())){
                  DailyPlan dailyPlan = new DailyPlan();
                  dailyPlan.setStaff(workTemplateExecutable.getStaff());
                  dailyPlan.setAssignedById(workTemplateExecutable.getInitiateProject().getStaffId());
                  dailyPlan.setTitle(workTemplateExecutable.getInitiateProject().getName() + "项目工作");
                  dailyPlan.setWork(workTemplateExecutable.getWorkTemplateContent().getContent());
                  dailyPlan.setStartTime(workTemplateExecutable.getInitiateProject().getStartTime());
                  dailyPlan.setEndTime(workTemplateExecutable.getInitiateProject().getStartTime());
                  dailyPlan.setEnable(0);
                  dailyPlan.setPriority(1);
                  dailyPlanService.insertPlan(dailyPlan);

              }else if ("每月一次".equals(workTemplateExecutable.getWorkTemplateContent().getTime())){
                  Date date1 = workTemplateExecutable.getInitiateProject().getStartTime();
                  Date date2 = workTemplateExecutable.getInitiateProject().getEndTime();
                  while (date2.getTime() >= date1.getTime()){
                      DailyPlan dailyPlan = new DailyPlan();
                      dailyPlan.setStaff(workTemplateExecutable.getStaff());
                      dailyPlan.setAssignedById(workTemplateExecutable.getInitiateProject().getStaffId());
                      dailyPlan.setTitle(workTemplateExecutable.getInitiateProject().getName() + "项目工作");
                      dailyPlan.setWork(workTemplateExecutable.getWorkTemplateContent().getContent());
                      dailyPlan.setStartTime(date1);
                      dailyPlan.setEndTime(date1);
                      dailyPlan.setEnable(0);
                      dailyPlan.setPriority(1);
                      Calendar c = Calendar.getInstance();
                      c.setTime(date1);
                      c.add(Calendar.MONTH, 1);
                      date1 = c.getTime();
                      dailyPlanService.insertPlan(dailyPlan);
                  }

              }else if("每天".equals(workTemplateExecutable.getWorkTemplateContent().getTime())){
                  int betweenDate = (int) ((workTemplateExecutable.getInitiateProject().getEndTime().getTime() - workTemplateExecutable.getInitiateProject().getStartTime().getTime())/(60*60*24*1000));
                  System.out.println(betweenDate);
                  for (int i = 0; i < betweenDate; i++) {
                      DailyPlan dailyPlan = new DailyPlan();
                      dailyPlan.setStaff(workTemplateExecutable.getStaff());
                      dailyPlan.setAssignedById(workTemplateExecutable.getInitiateProject().getStaffId());
                      dailyPlan.setTitle(workTemplateExecutable.getInitiateProject().getName() + "项目工作");
                      dailyPlan.setWork(workTemplateExecutable.getWorkTemplateContent().getContent());
                      dailyPlan.setStartTime(workTemplateExecutable.getInitiateProject().getStartTime());
                      dailyPlan.setEndTime(workTemplateExecutable.getInitiateProject().getStartTime());
                      dailyPlan.setEnable(0);
                      dailyPlan.setPriority(1);
                      dailyPlanService.insertPlan(dailyPlan);
                  }
              }
        });
    }
}
